import Dep, { pushTarget } from "./dep"
import { queueWatcher } from "./scheduler";
export function isWatcher(target) {
  if (target instanceof Watcher) return true;
  return false;
}
let uid = 0
export default class Watcher {
  constructor(vm, updateFn) {
    console.log('Watcher constructor');
    this.vm = vm
    this.updateFn = updateFn
    this.id = ++uid
    this.newDeps = []
    this.depIds = new Set()
    this.newDepIds = new Set()
    this.get()
  }
  get() {
    // 读一下当前key，触发依赖收集
    pushTarget(this)
    this.update();
  }
  addDep(dep) {
    const id = dep.id
    if (!this.newDepIds.has(id)) {
      this.newDepIds.add(id)
      this.newDeps.push(dep)
      if (!this.depIds.has(id)) {
        dep.addDep(this)
      }
    }
  }
  // 未来会被dep调
  update() {
    // const watch = this;
    // queueWatcher(watch)
    console.log('watcher update更新');
    const data = this.vm._data;
    this.updateFn.call(this.vm, data)
  }

}